﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class AchievementCell : MonoBehaviour {

    // 업적셀 프리펩
    static GameObject s_CellPrefab;
    // 업적셀 프리펩 리소스 위치
    const string CellPrefabPath = "Prefabs/Achievement/Achievement";
    // 업적셀 갯수
    static AchievementCell[] s_AchievementCellList;
    // 업적셀 최대 표현 갯수 이상의 업적 저장소
    static List<string> s_AchieveBodyQue;
    // 받은 업적 대기 큐
    static List<CAchievementData> s_AhieveDataQue;
    // 업적 분류 중복 방지
    static bool s_IsAchievedataSelecting = false;
    // 업적셀 최대 표현 갯수
    const int MaxOfShowAcheivement = 7;

    // 셀내 업적 표현 라벨
    public UILabel m_AchieveBodyLabel;
    public UILabel m_AchieveTitleLabel;

    int m_Index = 0;
    bool m_isNeedEndProcess = false;

    public static void AddAchievement(CAchievementData data)
    {
       // Debug.LogError("heeso----------------------------------  AddAchievement");

        if (s_AhieveDataQue == null)
            s_AhieveDataQue = new List<CAchievementData>();

        s_AhieveDataQue.Add(data);
    }
  
    public static void DoShowAchieve(BoardPerson person, string achieveName)
    {       
        if (BoardManager.Instance.boardPerson_me == person)
        {
           // Debug.LogError("heeso ---------------------------  DoShowAchieve");

            SelectAchievement(achieveName);
        }
    }

    static void SelectAchievement(string achieveName)
    {
        if(!s_IsAchievedataSelecting)
        {
            if (s_AhieveDataQue == null)
                s_AhieveDataQue = new List<CAchievementData>();

            s_IsAchievedataSelecting = true;

            List<CAchievementData> NeedShowAhieveData = new List<CAchievementData>();
           // Debug.LogError("heeso----------------------------------SelectAchievement");
           // Debug.LogError("heeso----------------------------------s_AhieveDataQue count = " + s_AhieveDataQue.Count);

            foreach (CAchievementData tempData in s_AhieveDataQue)
            {
               // Debug.LogError("heeso----------------------------------" + tempData.name + "   /   " + achieveName);

                if (tempData.name.Contains(achieveName))
                {
                    NeedShowAhieveData.Add(tempData);
                   // Debug.LogError("heeso----------------------------------NeedShowAhieveData.Add(tempData)");
                }
            }

            foreach (CAchievementData tempData in NeedShowAhieveData)
            {
                string kind = null;

                if (tempData.achieve_type == achieve_type.achievement)
                    kind = CStringTableManager.Instance.GetItemById(2369);
                else
                    kind = CStringTableManager.Instance.GetItemById(2370);

                string body = string.Format("{0}@{1}", CStringTableManager.Instance.GetItemByName(tempData.title_string), kind);

                ShowAchieveMent(body);//, tempData.achieve_type);
                s_AhieveDataQue.Remove(tempData);
            }

            s_IsAchievedataSelecting = false;
        }
    }


    static void ShowAchieveMent(string body)
    {  
        if (s_AchievementCellList == null)
            s_AchievementCellList = new AchievementCell[MaxOfShowAcheivement];

        if (s_AchieveBodyQue == null)
            s_AchieveBodyQue = new List<string>();

        bool isCreated = false;
        for (int i = 0; i < MaxOfShowAcheivement; i++)
        {
            if (s_AchievementCellList[i] == null)
            {
                CreateAchievementCell(i, body);
                isCreated = true;
                break;
            }
        }

        if (!isCreated)
        {
            s_AchieveBodyQue.Add(body);
        }
	}

    static void CreateAchievementCell(int index, string body)
    {
        if (s_CellPrefab == null)
            s_CellPrefab = Resources.Load(CellPrefabPath) as GameObject;

        GameObject tempCell = Instantiate(s_CellPrefab) as GameObject;
        GameObject UiRoot = GameObject.Find("UI Root (2D)/Camera");

        tempCell.transform.parent = UiRoot.transform;
        tempCell.transform.localScale = Vector3.one;
        tempCell.transform.localPosition = new Vector3(910, 290 - (index * 100), 0);

        int divideIndex = body.IndexOf("@");
        string bodyText = body.Remove(divideIndex);
        string titleText = body.Remove(0, divideIndex + 1);

        s_AchievementCellList[index] = tempCell.GetComponent("AchievementCell") as AchievementCell;
        s_AchievementCellList[index].m_AchieveBodyLabel.text = bodyText;
        s_AchievementCellList[index].m_AchieveTitleLabel.text = titleText;
        s_AchievementCellList[index].m_Index = index;
        s_AchievementCellList[index].StartShow();
    }

    void StartShow()
    {
        iTween.MoveTo(gameObject, iTween.Hash("islocal", true, "time", 1.0f, "x", 415, "oncompletetarget", gameObject, "oncomplete", "EndShow"));
    }

    void EndShow()
    {
        iTween.MoveTo(gameObject, iTween.Hash("islocal", true, "delay", 1.0f, "time", 1.0f, "x", 910, "oncompletetarget", gameObject, "oncomplete", "DestroyCell"));        
    }

    void DestroyCell()
    {       
        m_isNeedEndProcess = true;      
    }

    // 업적 저장소에 있는 업적 추가 표현
    void CheckAchieveBodyQue()
    {
        if (s_AchieveBodyQue.Count > 0)
        {
            string tempString = s_AchieveBodyQue[0];
            s_AchieveBodyQue.RemoveAt(0);
            ShowAchieveMent(tempString);
        }
    }

    void Update()
    {
        if(m_isNeedEndProcess)
        {
            Destroy(gameObject);
            s_AchievementCellList[m_Index] = null;            
            CheckAchieveBodyQue();
        }
    }	
}
